<?php
namespace app\admin\controller;

use app\common\model\AddressModel;
use app\common\model\GoodsModel;
use app\common\model\GoodsOrderModel;
use app\common\model\GoodsSpecGoodsPriceModel;
use app\common\model\MemberModel;
use app\common\model\RewardModel;
use app\common\model\StoreModel;
use app\common\service\MsgService;
use common\util\ArrayUtil;
use common\util\Tools;
use think\Exception;
use think\Log;
use web\util\WebUtil;

class Order extends Base {

    /*
     * 初始化操作
     */
    public function _initialize() {
        $this->needLogin();
    }

    // 订单列表
    public function index() {
        $status = $this->request->param('status');
        $mid = $this->request->param('mid',0);
        $this->assign('status',$status);
        $this->assign('mid',$mid);
        return $this->fetch();
    }

    // 订单详情
    public function detail() {
        $id  = $this->request->param('id');
        $order = GoodsOrderModel::instance()->find($id);
        $receive_addr_id = $order['receive_addr_id'];
        $send_addr_id = $order['send_addr_id'];
        
        $order = $order->toArray();
        $sender = [
            'sender_name'=>'',
            'sender_tel'=>'',
            'sender_addrvalue'=>'',
            'sender_addr'=>'',
        ];
        if($send_addr_id){
            $temp = AddressModel::instance()->find($send_addr_id)->toArray();
            $sender = [
                'sender_name'=>$temp['name'],
                'sender_tel'=>$temp['tel'],
                'sender_addrvalue'=>$temp['addrvalue'],
                'sender_addr'=>$temp['addr'],
            ];
        }
        $order['status_name'] = WebUtil::getOrderStatusName($order['ispay']); 
        $order = array_merge($order,$sender);
        $member = MemberModel::instance()->find($order['mid']);
        $order['name'] = $member['name'];
        
        $this->assign('item',$order);
        
        
        return $this->fetch();
    }

    /**
     * 订单列表
     */
    public function rs() {
        
        $length = 10;
        $draw = $this->request->param('draw',1);
        $start = $this->request->param('start',0);
        $ispay = $this->request->param('ispay',0);
        $status = $this->request->param('status','');
        $oid = $this->request->param('oid','');
        $name = $this->request->param('name','');
        $tel = $this->request->param('tel','');
        $receive_name = $this->request->param('receive_name','');
        $express_no = $this->request->param('express_no','');
        $flag = $this->request->param('flag','');
        
        $mid = $this->request->param('mid',0);
        $goodsname = $this->request->param('goodsname','');
        $goodsid = $this->request->param('goodsid',0);
        $startTime = $this->request->param('startTime','');
        $endTime = $this->request->param('endTime','');
        $where = [];
        
        if($startTime && $endTime){
            $where['ctime'] = ['between',[strtotime($startTime),strtotime($endTime) + 86399]];
        }else{
            if($startTime){
                $where['ctime'] = ['>=',strtotime($startTime)];
            }
            if($endTime){
                $where['ctime'] = ['<=',strtotime($endTime) + 86399];
            }
        }
        
        if($name){
            $mids = MemberModel::instance()->searchMidsByKeywords($name);
            $where['mid']= ['in',$mids];
        }
       
        if($status){
            $where['ispay'] = ['in',$status];
        }
        if($ispay){
            $where['ispay'] = $ispay;
        }
        if($flag != ''){
            $where['flag'] = $flag;
        }
        if($oid){
            $where['oid'] = $oid;
        }
        if($tel){
            $where['receive_tel'] = ['like','%'.$tel.'%'];
        }
        
        $goodsname && $where['goodsname'] = ['like','%'.$goodsname.'%'];
        $receive_name && $where['receive_name'] = ['like','%'.$receive_name.'%'];
        $goodsid && $where['goodsid'] = $goodsid;
        if($express_no){
            $where['express_no'] = $express_no;
        }
        if($mid){
            $where['mid'] = $mid;
        }
        $items= GoodsOrderModel::instance()->where($where)->order('id','desc')->limit($start,$length)->select();
        $count=  GoodsOrderModel::instance()->where($where)->count();
        
        $mids = ArrayUtil::getKeyValuesFromArray($items, 'mid');
        $midsMap = [];
        if($mids){
            $midsMap = MemberModel::instance()->getItemsByIds($mids,'name,grade');
        }
        $storeIds = ArrayUtil::getKeyValuesFromArray($items, 'store_id');
        $storeMap = [];
        if($storeIds){
            $storeMap = StoreModel::instance()->getItemsByIds($storeIds,'name');
        }
        $list = [];
        foreach ($items as $item){
            $mid = $item['mid'];
            $list [] = [
                'id'=>$item['id'],
                'mid'=> $item['mid'],
                'oid'=> $item['oid'],
                'name'=>$midsMap[$mid]['name'],
                'level'=>WebUtil::getGradeName($midsMap[$mid]['grade']),
                'goods_name'=> $item['goodsname'],
                'num'=> $item['courier_num'],
                'goods_price'=> $item['sigprice'],
                'total'=>$item['total'],
                'receive_name'=>$item['receive_name'],
                'receive_phone'=>$item['receive_tel'],
                'receive_addr'=>$item['receive_addr'],
                'send_addr'=>'发货人地址',
                'express_name'=>$item['express_name'],
                'express_no'=>$item['express_no'],
                'remark'=>$item['beizhu'],
                'ctime'=>$item['addtime'],
                'status_name'=>WebUtil::getOrderStatusName($item['ispay']),
                'status'=>$item['ispay'],
                'flag'=>$item['flag'],
                'tel'=>$item['receive_tel'],
                'store_name'=>ArrayUtil::getValueFromMap($storeMap, $item['store_id']),
            ];
        }
        
        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
    }

    /**
     *  发货
     */
    public function send(){
        $id = $this->request->param('id');
        $express_name = $this->request->param('express_name');
        $express_no = $this->request->param('express_no');
        try {
            $this->_updateOrder($id,$express_name,$express_no);
            return $this->returnResult(1, '发货通过');
        } catch (\Exception $e) {
            return $this->returnResult(-1, $e->getMessage());
        }
        
    }
    
    private function _updateOrder($id,$express_name,$express_no){
        $order = GoodsOrderModel::instance()->find($id);
        if(empty($order)){
            throw new Exception('订单不存在');
        }
        if($order['ispay'] != 1){//
            throw new Exception('订单已经审核');
        }
        //更新订单
        $orderUpdate = [
            'id'=>$id,
            'express_name'=>$express_name,
            'express_no'=>$express_no,
            'ispay'=>3,
            'updatetime'=> Tools::getCurDate(),
        ];
        //修改订单
        GoodsOrderModel::instance()->update($orderUpdate);
        
        $mid = $order['mid'];//用户id
        $num = $order['courier_num'];//购买个数
        $total = $order['total'];//订单金额
        $account = $order['account'];
        $reward_account = $order['reward_account'];
        
        //修改用户锁定金额,减少锁定
        MemberModel::instance()->where('id',$mid)
        ->dec('account_lock',$account)
        ->dec('rewardaccount_lock',$reward_account)->update();
        $adddate = $order['addtime'];
        $date = date('Ymd',strtotime($adddate));
        $month = date('Ym',strtotime($adddate));
        
        $member= MemberModel::instance()->find($mid);
        //订单奖励,如果有上级就奖励上级
        if(!empty($member['fid'])){
            //给上级分积分
            $fid = $member['fid'];
            $rewardPrice = $order['reward'];//单品奖励价格
            $display_name = $member['name'];
            $money = $num * $rewardPrice;
            $rewardData = [
                'mid'=> $fid,
                'sub_mid'=>$mid,
                'type'=>1,
                'display_name'=>$display_name,
                'rem_tel'=>$member['tel'],
                'num'=>$num,
                'value'=>$rewardPrice,
                'ctime'=>time(),
                'utime'=>time(),
                'date'=>$date,
                'month'=>$month,
                'status'=> 0,
                'settlement_time'=> 0,
                'goodsorder_id'=>$id,
                'money'=> $money
            ];
            RewardModel::instance()->insertGetId($rewardData);
            //奖励金结算到账户
//             MemberModel::instance()->where('id',$fid)->setInc('rewardaccount',$money);
            
            $pmember = MemberModel::instance()->find($fid);
            if($pmember['openid']){
                MsgService::sendOrderRewardMsg($pmember['openid'], $order['goodsname'], $total, $money);
            }
        }
        
        //团队奖励
        $pid_path = $member['pid_path'];
        //父级团队个数都加num
        $pids = explode(',', $pid_path);
        $pids = array_filter($pids);
        $pids[] = $mid;
        foreach ($pids as $pid){
            //             $month = date('Ym');
            $where = ['month'=>$month,'mid'=>$pid,'type'=>2];
            $count = RewardModel::instance()->where($where)->count();
            if($count > 0){//更新
                RewardModel::instance()->where($where)->setInc('num',$num);
            }else{
                $rewardData = [
                    'mid'=> $pid,
                    'sub_mid'=>$mid,
                    'type'=>2,
                    'display_name'=>'团队奖',
                    'num'=>$num,
                    'ctime'=>time(),
                    'status'=>0,
                    'month'=>$month,
                    'date'=>$date,
                ];
                RewardModel::instance()->insertGetId($rewardData);
            }
        }
        //领导积分奖励
        $lists = MemberModel::instance()->getItemsByIds($pids,'grade');
        
        foreach ($lists as $key=>$grade){
            $pid = $key;
            if($grade == 2){
                //                 $month = date('Ym');
                $where = ['month'=>$month,'mid'=>$pid,'type'=>3];
                $count = RewardModel::instance()->where($where)->count();
                if($count > 0){//更新
                    RewardModel::instance()->where($where)->setInc('num',$num);
                }else{
                    $rewardData = [
                        'mid'=> $pid,
                        'sub_mid'=>$mid,
                        'type'=>3,
                        'display_name'=>'领导积分奖',
                        'num'=>$num,
                        'ctime'=>time(),
                        'status'=>0,
                        'month'=>$month,
                        'date'=> $date,
                    ];
                    RewardModel::instance()->insertGetId($rewardData);
                }
            }
        }
        return true;
    }
    
    public function update_express(){
        $id = $this->request->param('id');
        $express_name = $this->request->param('express_name','');
        $express_no = $this->request->param('express_no','');
        
        $order = GoodsOrderModel::instance()->find($id);
        if(empty($order)){
            return $this->returnResult(-1, '订单不存在');
        }
        if($order['ispay'] != 3){//
            return $this->returnResult(-1, '订单未发货');
        }
        $data =[
            'id' =>$id,
            'express_name' =>$express_name,
            'express_no' =>$express_no,
            'updatetime'=>Tools::getCurDate(),
        ];
        GoodsOrderModel::instance()->update($data);
        return $this->returnResult(1,'更新成功');
    }
    
    /**
     * 取消
     */
    public function cancel(){
        
        $id = $this->request->param('id');
        $order = GoodsOrderModel::instance()->find($id);
        if(empty($order)){
            return $this->returnResult(-1, '订单不存在');
        }
        if($order['ispay'] != 1 ){//
            return $this->returnResult(-1, '订单已经审核');
        }
//         if($order['flag'] == 1){//
//             return $this->returnResult(-1, '订单已经在发货中,不能取消');
//         }
        //更新订单
        $orderUpdate = [
            'id'=>$id,
            'ispay'=>2,//取消
            'updatetime'=> Tools::getCurDate(),
        ];
        //修改订单
        GoodsOrderModel::instance()->update($orderUpdate);
        
        $mid = $order['mid'];//用户id
        $total = floatval($order['total']);//订单金额
        $num = $order['courier_num'];
        $account = $order['account'];
        $reward_account = $order['reward_account'];
        $goodsId = $order['goodsid'];
        
        //修改用户锁定金额,减少锁定,返回账号
        MemberModel::instance()->where('id',$mid)
                                ->dec('account_lock',$account)->inc('account',$account)
                                ->dec('rewardaccount_lock',$reward_account)->inc('rewardaccount',$reward_account)
                                ->update();
        //更新库存和销售量
        GoodsModel::instance()->where('id',$goodsId)->dec('sales',$num)->inc('stock',$num)->update();
        $key = $order['key'];
        $key && GoodsSpecGoodsPriceModel::where('goods_id', $goodsId)->where('key', $key)
        ->inc('store_count', $num)
        ->update();
        
        $member = MemberModel::instance()->find($mid);
        if($member['openid']){
            MsgService::sendOrderCancelMsg($member['openid'], $id, $order['oid'], $total);
            MsgService::sendAccountChangeMsg($member['openid'], $total,$order['oid']);
        }
        return $this->returnResult(1,'取消完成');
    }
    
    //TODO 退款逻辑
    /**
     * 退款 
     * 
     * @return \think\response\Json
     */
    public function refund(){
        $id = $this->request->param('id');
        $order = GoodsOrderModel::instance()->find($id);
        if(empty($order)){
            return $this->returnResult(-1, '订单不存在');
        }
        if($order['ispay'] == 2 ){//
            return $this->returnResult(-1, '订单已经取消');
        }
        if($order['ispay'] == 5 ){//
            return $this->returnResult(-1, '订单已经完成无法退款');
        }
        
        //更新订单
        $orderUpdate = [
            'id'=>$id,
            'ispay'=>2,//取消
            'updatetime'=> Tools::getCurDate(),
        ];
        //修改订单
        GoodsOrderModel::instance()->update($orderUpdate);
        
        $mid = $order['mid'];//用户id
        $total = floatval($order['total']);//订单金额
        $num = $order['courier_num'];
        $account = $order['account'];
        $reward_account = $order['reward_account'];
        $goodsId = $order['goodsid'];
        
        $member = MemberModel::instance()->find($mid);
        
        //修改用户锁定金额,减少锁定,返回账号
        if($order['ispay'] == 1){//未发货
            MemberModel::instance()->where('id',$mid)
            ->dec('account_lock',$account)
            ->inc('account',$account)
            ->dec('rewardaccount_lock',$reward_account)
            ->inc('rewardaccount',$reward_account)
            ->update();
        }else if($order['ispay'] == 3){
            //已经发货
            MemberModel::instance()->where('id',$mid)
            ->inc('account',$account)
            ->inc('rewardaccount',$reward_account)
            ->update();
            //删除奖励
            //直属奖励
            $record = RewardModel::instance()->where(['goodsorder_id'=>$id])->find();
            if($record['status'] == 0){//没有结算
                RewardModel::instance()->where(['goodsorder_id'=>$id])->delete();
                $pid_path = $member['pid_path'];
                //父级团队个数都加num
                $pids = explode(',', $pid_path);
                $pids = array_filter($pids);
                $pids[] = $mid;
                $rewardWhere = [
                    'month' => $record['month'],
                    'type' => ['in',[2,3]],
                    'mid'=>['in',$pids]
                ];
                RewardModel::instance()->where($rewardWhere)->dec('num',$num)->update();
            }else{
                
            }
        }
        
        //更新库存和销售量
        GoodsModel::instance()->where('id',$goodsId)->dec('sales',$num)->inc('stock',$num)->update();
        $key = $order['key'];
        $key && GoodsSpecGoodsPriceModel::where('goods_id', $goodsId)->where('key', $key)
        ->inc('store_count', $num)
        ->update();
        
        if($member['openid']){
            MsgService::sendOrderCancelMsg($member['openid'], $id, $order['oid'], $total);
            MsgService::sendAccountChangeMsg($member['openid'], $total,$order['oid']);
        }
        return $this->returnResult(1,'退款完成');
    }
   
    
    
    
    /**
     * 完成
     */
    public function finish(){
        
        $id = $this->request->param('id');
        $order = GoodsOrderModel::instance()->find($id);
        if(empty($order)){
            return $this->returnResult(-1, '订单不存在');
        }
        if($order['ispay'] != 3){//只有已发货的订单才能设置完成
            return $this->returnResult(-1, '订单未发货,不能完成');
        }
        
        //更新订单
        $orderUpdate = [
            'id'=>$id,
            'ispay'=>5,//完成
            'updatetime'=> Tools::getCurDate(),
        ];
        //修改订单
        GoodsOrderModel::instance()->update($orderUpdate);
        
        return $this->returnResult(1,'完成');
    }
    
    public function export(){
        
        $status = $this->request->param('status',0);
        $oid = $this->request->param('oid','');
        $name = $this->request->param('name','');
        $tel = $this->request->param('tel','');
        $flag = $this->request->param('flag','');
        
        $startTime = $this->request->param('startTime','');
        $endTime = $this->request->param('endTime','');
        $where = [];
        
        if($startTime && $endTime){
            $where['t.ctime'] = ['between',[strtotime($startTime),strtotime($endTime) + 86399]];
        }else{
            if($startTime){
                $where['t.ctime'] = ['>=',strtotime($startTime)];
            }
            if($endTime){
                $where['t.ctime'] = ['<=',strtotime($endTime) + 86399];
            }
        }
        
        if($name){
            $mids = MemberModel::instance()->where('name','like','%'.$name.'%')->column('id');
            if($mids){
                $where['mid']= ['in',$mids];
            }
        }
        
        if($status){
            $where['t.ispay'] = $status;
        }
        if($oid){
            $where['t.oid'] = $oid;
        }
        
        if($tel){
            $where['receive_tel'] = ['like','%'.$tel.'%'];
        }
        if($flag != ''){
            $where['t.flag'] = $flag;
        }
        
        $items= GoodsOrderModel::instance()->field($this->_getField())
        ->alias('t')
//         ->join('address t1','t.receive_addr_id = t1.id','left')
        ->join('address t2','t.send_addr_id = t2.id','left')
        ->where($where)->order('t.id','desc')
        ->select();
        
        
        $storeMap = StoreModel::instance()->column('name','id');
        $index = 1;
        foreach($items as &$item){
            $item['store'] = '';
            if($item['store_id']){
                $item['store'] = $storeMap[$item['store_id']];
            }
            $item['id'] = $index++;
        }
        $name = 'order-'.date('YmdHi').'.xlsx';
        $this->_exportExl($name,$items);
    }
    
    private function _getField(){
         return "t.id,t.addtime,t.oid as '订单号',concat(goodsname,' ',key_name) as '产品名称',courier_num '数量',total '金额',
                t.receive_name '收货人姓名',t.receive_tel '收货人电话',concat(t.receive_addrvalue,t.receive_addr) '收货人地址',
                IFNULL(t2.name,'') '发货人',IFNULL(t2.tel,'') '发货人电话',IFNULL(concat(t2.addrvalue,t2.addr),'') '发货人地址',
                beizhu '备注',express_name '快递公司',express_no '快递单号',t.store_id";
    }

    public function export_express(){
        return $this->error('不可使用');
        $hour = date('H');
        $h = 12;
        if($hour < $h){
//             return $this->error("没到".$h."点不能导出",'/admin/order/index.html?status=0');
        }
        $name = 'order_express-'.date('Ymd').'.xlsx';
        
        $file = $this->_getOrderFilePath($name);
        if(false){
            $phpexecl = readExcel($file);

            $objWriter = \PHPExcel_IOFactory::createWriter($phpexecl, 'Excel2007');
            exportExcel($objWriter,$name);
            exit;
        }else{
            $date = date("Y-m-d H:i:s",time() - 3600 * 2);

            $storeMap = StoreModel::instance()->column('name','id');
            $where = [];
            $where['t.ispay'] = 1;
            $where['t.flag'] = 0;
            $where['t.addtime'] = ['<=',$date];
            $list = GoodsOrderModel::instance()->field($this->_getField())
            ->alias('t')
//             ->join('address t1','t.receive_addr_id = t1.id','left')
            ->join('address t2','t.send_addr_id = t2.id','left')
            ->where($where)->order('t.id','desc')
            ->select();
            if($list){
                $ids = ArrayUtil::getKeyValuesFromArray($list, 'id');
                GoodsOrderModel::instance()->whereIn('id',$ids)->update(['flag'=>1]);
                Log::write("export_express ids:".implode(',', $ids));
            }
            $index = 1;
            foreach($list as &$item){
                $item['store'] = '';
                if($item['store_id']){
                    $item['store'] = $storeMap[$item['store_id']];
                }
                $item['id'] = $index++;
            }
            $this->_exportExl($name, $list);
        }
        
    }
    
    private function _exportExl($file, $list) {
        $excel = new \PHPExcel();
        $excel->setActiveSheetIndex(0);
        $nameValueMap = [
            'ID' => 'id',
            '订单号' => '订单号',
            '下单时间' => 'addtime',
            '产品名称' => '产品名称',
            '数量' => '数量',
            '金额' => '金额',
            '收货人姓名' => '收货人姓名',
            '收货人电话' => '收货人电话',
            '收货人地址' => '收货人地址',
            '发货人' => '发货人',
            '发货人电话' => '发货人电话',
            '发货人地址' => '发货人地址',
            '备注' => '备注',
            '快递公司' => '快递公司',
            '快递单号' => '快递单号',
            '仓库'=>'store',
        ];
        
        $sums = [];
        $index = 0;
        $nameKeys = array_keys($nameValueMap);
        foreach ($nameKeys as $nameKey) {
            $excel->getActiveSheet()->setCellValue(chr($index + 65) . '1', $nameKey,
                \PHPExcel_Cell_DataType::TYPE_STRING2); //
                $index ++;
        }
        $valueKeys = array_values($nameValueMap);
        
        $j = 2;
        foreach ($list as $k => $v) {
            $index = 0;
            foreach ($valueKeys as $valueKey) {
                if (is_string($v[$valueKey])) {
                    $excel->getActiveSheet()->setCellValueExplicit(chr($index + 65) . $j,
                        $v[$valueKey], \PHPExcel_Cell_DataType::TYPE_STRING);
                } else {
                    $excel->getActiveSheet()->setCellValue(chr($index + 65) . $j, $v[$valueKey]);
                }
                
                if (array_key_exists($valueKey, $sums)) {
                    $sums[$valueKey]['sum'] += $v[$valueKey];
                    if ($sums[$valueKey]['index'] == - 1) {
                        $sums[$valueKey]['index'] = $index;
                    }
                }
                $index ++;
            }
            $j ++;
        }
        foreach ($sums as $key => $value) {
            $excel->getActiveSheet()->setCellValue(chr($value['index'] + 65) . $j, $value['sum']);
        }
        
        $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
//         $objWriter->save($this->_getOrderFilePath($file));
        exportExcel($objWriter,$file);
    }
    
    private function _getOrderFilePath($file){
        return $file;
    }
    
    
    public function upload(){
        ini_set('max_execution_time', '0');
        $sucessList = [];
        $failList = [];
        $unprocessList = [];
        $processList = [];
        $count = 0;
        $isUpload = 0;
        if($this->request->isPost()){
            $isUpload = 1;
            // 获取表单上传文件
            $file = $this->request->file('file');
            
            if($file){
                try {
                    
                    $filepath = $file->getInfo('tmp_name');
                    $excel =  readExcel($filepath);
                    $currentSheet = $excel->getSheet(0);
                    /**取得一共有多少行*/
                    $allRow = $currentSheet->getHighestRow();
                    
                    $list = [];
                    $cols = ['B','J','L','M'];
                    
                    $oidAddr = $cols[0].'1';
                    $oidName = $currentSheet->getCell($oidAddr)->getValue();
                    
                    $expressNameAddr = $cols[2].'1';
                    $expressName = $currentSheet->getCell($expressNameAddr)->getValue();
                    if($expressName instanceof \PHPExcel_RichText)
                        $expressName = $expressName->__toString();
                    
                    $expressNoAddr = $cols[3].'1';
                    $expressNo = $currentSheet->getCell($expressNoAddr)->getValue();
                    
                    if($expressNo instanceof \PHPExcel_RichText)
                        $expressNo = $expressNo->__toString();
                    
                    $oidName = trim($oidName);
                    $expressNo = trim($expressNo);
                    $expressNo = trim($expressNo);
                    if($oidName !='订单号' || $expressName != '物流' || $expressNo!='单号' ){
                        throw new Exception('B L M 列不是订单号,物流和单号');   
                    }
                    //循环读取每个单元格的内容。注意行从1开始，列从A开始
                    for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){
                        
                        $item = [];
                        foreach ($cols as $col){
                            $addr = $col.$rowIndex;
                            $cell = $currentSheet->getCell($addr)->getValue();
                            if($cell instanceof \PHPExcel_RichText)     //富文本转换字符串
                                $cell = $cell->__toString();
                            $item[] = $cell;
                        }
                        $list []= $item;
                    }
                    $count = count($list);
                    foreach ($list as $item){
                        $oid = $item[0];
                        $express_name = $item[2];
                        $express_no = $item[3];
                        if(empty($express_no)){
                            $unprocessList [] = $oid;
                            Log::write("oid:$oid do not hava express_no");
                            continue;
                        }
                        $order = GoodsOrderModel::instance()->where('oid',$oid)->find();
                        if($order){
                            $id = $order['id'];
                            if($order['ispay'] == 1){
                                try {
                                    $this->_updateOrder($id, $express_name, $express_no);
                                    $sucessList [] = $oid;
                                } catch (\Exception $e) {
                                    $failList [] = $oid;
                                    Log::write("update orderId:$id error msg:".$e->getMessage());
                                }
                            }else{
                                $processList[] = $oid;
                                Log::write("update orderId:$id has process");
                            }
                        }
                    }
                    
                } catch (\Exception $e) {
                    $this->error('失败.原因:'.$e->getMessage());
                }
            }
        }
        $this->assign('sucess_list',$sucessList);
        $this->assign('fail_list',$failList);
        $this->assign('unprocess_list',$unprocessList);
        $this->assign('process_list',$processList);
        $this->assign('count',$count);
        $this->assign('is_uplaod',$isUpload);
        return $this->fetch();
    }
    
    
}
